package com.shuai.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.shuai.stickynote.MemoryNote;
import com.shuai.stickynote.R;
import com.shuai.tool.MyTime;

import java.util.ArrayList;

public class MemoryListAdapter extends RecyclerView.Adapter<MemoryListAdapter.BaseViewHolder>{
    private Context context;
    private ArrayList<ArrayList<MemoryNote>> dataList;
    private OnRecyItemClickListener onRecyItemClickListener;
    private OnRecyItemLongClickListener onRecyItemLongClickListener;

    public MemoryListAdapter(Context context) {
        this.context = context;
    }

    public void replaceAll(ArrayList<ArrayList<MemoryNote>> data) {
        if (dataList == null) dataList = new ArrayList<>();
        dataList.clear();
        if (data != null && data.size() > 0) dataList.addAll(data);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new OneViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_memory_list,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull final BaseViewHolder holder, int position) {
        holder.setData(dataList.get(position),position);
        if (onRecyItemClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int i = holder.getLayoutPosition();
                    onRecyItemClickListener.onClick(holder.itemView,i);
                }
            });
        }
        if (onRecyItemLongClickListener != null){
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int i = holder.getLayoutPosition();
                    onRecyItemLongClickListener.onClick(holder.itemView,i);
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public class BaseViewHolder extends RecyclerView.ViewHolder{
        public BaseViewHolder(@NonNull View itemView) {
            super(itemView);
        }
        void setData(ArrayList<MemoryNote> data, int position){}
    }

    private class OneViewHolder extends BaseViewHolder{
        private ListView lv_list;
        private TextView tv_time;
        private MemoryListChildAdapter adapter;

        public OneViewHolder(@NonNull View itemView) {
            super(itemView);
            lv_list = itemView.findViewById(R.id.adapter_memory_list);
            tv_time = itemView.findViewById(R.id.adapter_memory_time);
        }

        @Override
        void setData(ArrayList<MemoryNote> data, int position) {
            super.setData(data, position);
            adapter = new MemoryListChildAdapter(context);
            adapter.replaceAll(data);
            lv_list.setAdapter(adapter);
            tv_time.setText(MyTime.toDateString(data.get(0).getTime()));
        }
    }
    public interface OnRecyItemClickListener{
        void onClick(View view, int position);
    }
    public interface OnRecyItemLongClickListener{
        void onClick(View view, int position);
    }
    public void setOnRecyItemClickListener(OnRecyItemClickListener onRecyItemClickListener) {
        this.onRecyItemClickListener = onRecyItemClickListener;
    }
    public void setOnRecyItemLongClickListener(OnRecyItemLongClickListener onRecyItemLongClickListener) {
        this.onRecyItemLongClickListener = onRecyItemLongClickListener;
    }
}
